<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="sku名称" prop="skuName">
        <el-input
          v-model="queryParams.skuName"
          placeholder="请输入sku名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="skuPrice">
        <el-input
          v-model="queryParams.skuPrice"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:sku:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:sku:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:sku:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:sku:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="skuList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="sku主键" align="center" prop="skuId" />
      <el-table-column label="sku名称" align="center" prop="skuName" />
      <el-table-column label="状态" align="center" prop="saleStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.saleStatus==1">上架</span>
          <span v-if="scope.row.saleStatus==2">下架</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="skuImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.skuImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="skuPrice" />
      <el-table-column label="商品描述" align="center" prop="skuDescription" />
      <el-table-column label="销售量" align="center" prop="saleCount" />
      <el-table-column label="商品数量" align="center" prop="productCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:sku:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['product:sku:remove']"-->
<!--          >删除</el-button>-->
          <el-button type="danger" @click="openShopping(scope.row)">加入购物车</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改sku商品套餐 例：苹果14 幻影紫 16+128G对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="sku名称" prop="skuName">
          <el-input v-model="form.skuName" placeholder="请输入sku名称" />
        </el-form-item>
        <el-form-item label="spu商品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入spu商品id" />
        </el-form-item>
        <el-form-item label="图片" prop="skuImg">
          <image-upload v-model="form.skuImg"/>
        </el-form-item>
        <el-form-item label="价格" prop="skuPrice">
          <el-input v-model="form.skuPrice" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="商品描述" prop="skuDescription">
          <el-input v-model="form.skuDescription" placeholder="请输入商品描述" />
        </el-form-item>
        <el-form-item label="销售量" prop="saleCount">
          <el-input v-model="form.saleCount" placeholder="请输入销售量" />
        </el-form-item>
        <el-form-item label="商品数量" prop="productCount">
          <el-input v-model="form.productCount" placeholder="请输入商品数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    加入购物车对话框-->
    <el-dialog
      title="购物车"
      :visible.sync="dialogShoppingCard"
      width="500px"
    :before-close="handleClose">
      {{shoppingReq}}
      <el-form ref="form" :model="shoppingReq" :rules="rules" label-width="80px">
        <el-form-item label="sku名称">
          <span>{{ shoppingReq.skuName }}</span>
        </el-form-item>
        <el-form-item label="图片">
          <img :src="shoppingReq.skuImg" width="300px" height="300px" alt="加载失败">
        </el-form-item>
        <el-form-item label="购买数量">
          <el-input-number v-model="shoppingReq.skuNum" @change="handleChange" :value="1" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="总价">
          <el-input v-model="shoppingReq.cartTotalPrice"  disabled></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogShoppingCard = false">取消</el-button>
        <el-button type="primary" @click="cartInsert">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getSku, delSku, addSku, updateSku, findByProductId} from "@/api/product/sku";
import {getInfo} from "@/api/login";
import {toCartInsert} from "@/api/shopping/shoppingCartList";

export default {
  name: "Sku",
  data() {
    return {
      //获取当前用户Id
      userId:null,
      //购物车对话框
      dialogShoppingCard:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // sku商品套餐 例：苹果14 幻影紫 16+128G表格数据
      skuList: [],
      //购物车参数
      shoppingReq: {
        skuId: null,
        skuName: "",
        cartPrice: null,
        skuNum: 1,
        cartTotalPrice: null,
        userId:null
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skuName: null,
        saleStatus: null,
        skuPrice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 绑定商品的spu
      productId: null
    };
  },
  created() {
    this.getUserInfo();
    this.getList();
  },
  methods: {
    /** 查询sku商品套餐 例：苹果14 幻影紫 16+128G列表 */
    getList() {
      this.loading = true;
      this.productId=this.$route.query.productId;
      // console.log(this.productId);
      // listSku(this.queryParams).then(response => {
      //   this.skuList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      findByProductId(this.productId).then(res=>{
        // console.log(res);
        this.skuList=res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        skuId: null,
        skuName: null,
        productId: null,
        saleStatus: null,
        skuImg: null,
        skuPrice: null,
        skuDescription: null,
        saleCount: null,
        productCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.skuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加sku商品套餐 例：苹果14 幻影紫 16+128G";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const skuId = row.skuId || this.ids
      getSku(skuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改sku商品套餐 例：苹果14 幻影紫 16+128G";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.skuId != null) {
            updateSku(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSku(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const skuIds = row.skuId || this.ids;
      this.$modal.confirm('是否确认删除sku商品套餐 例：苹果14 幻影紫 16+128G编号为"' + skuIds + '"的数据项？').then(function() {
        return delSku(skuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/sku/export', {
        ...this.queryParams
      }, `sku_${new Date().getTime()}.xlsx`)
    },
    handleClose(done){
      this.$confirm('确认关闭')
        .then(_=>{
          done();
        })
        .catch(_=>{});

    },
    /** 打开购物车对话框*/
    openShopping(sku){
      this.shoppingReq.skuId=sku.skuId;
      this.shoppingReq.skuName=sku.skuName;
      this.shoppingReq.cartPrice=sku.skuPrice;
      this.shoppingReq.skuNum=1;
      this.shoppingReq.userId=this.userId;
      this.shoppingReq.cartTotalPrice=sku.skuPrice;
      this.dialogShoppingCard=true;
    },
    handleChange(value){
      this.shoppingReq.cartTotalPrice=this.shoppingReq.skuNum*this.shoppingReq.cartPrice
    },
    getUserInfo(){
      getInfo().then(res=>{
        this.userId=res.data.user.userId;
      })
    },
    cartInsert(){
      toCartInsert(this.shoppingReq).then(res=>{
        if(res.code===200){
          this.$message.success(res.data);
          this.dialogShoppingCard=false;
        }else {
          this.$message.error(res.message);
        }

      })
    }
  }
};
</script>
